/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InitDialog.java
 *
 * Created on Oct 30, 2011, 12:04:41 AM
 */
package aor2.gui.cpu;

import aor2.assembly.Assembler;
import aor2.cpu.ClockCPU;
import aor2.cpu.InternBus;
import aor2.cpu.blockcontrol.ConditionSignalSet;
import aor2.cpu.blockcontrol.ControlSignalSet;
import aor2.cpu.blockcontrol.horizontal.ControlUnitHorizontal2Type;
import aor2.cpu.blockinterface.Arbitrator;
import aor2.cpu.blockinterface.BusOvertake;
import aor2.cpu.blockinterface.InterfaceBlock;
import aor2.cpu.blockinterface.SynchronizationBlock;
import aor2.cpu.blockinterrupt.InterruptBlock;
import aor2.cpu.blockinterrupt.InterruptSignalSet;
import aor2.cpu.blockoperation.AlushiftXYM1;
import aor2.cpu.blockoperation.Psw;
import aor2.cpu.blockregisters.GPRegisters;
import aor2.cpu.blockregisters.InstructionRegister;
import aor2.cpu.blockregisters.TempRegisters;
import aor2.systembus.ADbus;
import aor2.systembus.SystemBus;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 * 
 * @author Maki
 */
public class InitDialog extends javax.swing.JDialog {

	/** Creates new form InitDialog */
	public InitDialog(javax.swing.JFrame parent, boolean modal) {
		super(parent, modal);
                setLocation(300,150);
		initComponents();
		this.setVisible(modal);

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        init_file = new javax.swing.JFileChooser();
        registryDIalog = new javax.swing.JDialog();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jTextField7 = new javax.swing.JTextField();
        jTextField8 = new javax.swing.JTextField();
        jTextField9 = new javax.swing.JTextField();
        jButton6 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jTextField11 = new javax.swing.JTextField();
        memoryDialog = new javax.swing.JDialog();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTextField10 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        pomeriAdresu = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        radioProgram = new javax.swing.JRadioButton();
        radioFajl = new javax.swing.JRadioButton();
        src = new javax.swing.JTextField();
        open = new javax.swing.JButton();
        exit = new javax.swing.JButton();
        simButton = new javax.swing.JButton();
        initReg = new javax.swing.JButton();
        initMem = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        init_file.setEnabled(false);

        registryDIalog.setTitle("Registri");
        registryDIalog.setAlwaysOnTop(true);
        registryDIalog.setModal(true);
        registryDIalog.setName("registryDialog"); // NOI18N
        registryDIalog.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                registryDIalogWindowClosed(evt);
            }
        });

        jButton6.setText("Sacuvaj");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("R0");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("R1");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("R2");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("R3");

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("R4");

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("R5");

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("R6 (SP)");

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel8.setText("R7 (PC)");

        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel10.setText("IMR");

        javax.swing.GroupLayout registryDIalogLayout = new javax.swing.GroupLayout(registryDIalog.getContentPane());
        registryDIalog.getContentPane().setLayout(registryDIalogLayout);
        registryDIalogLayout.setHorizontalGroup(
            registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(registryDIalogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(registryDIalogLayout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addGroup(registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)))
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(registryDIalogLayout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField11, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE)
                    .addComponent(jTextField9)
                    .addComponent(jTextField8)
                    .addComponent(jTextField7)
                    .addComponent(jTextField6)
                    .addComponent(jTextField5)
                    .addComponent(jTextField4)
                    .addComponent(jTextField3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(51, 51, 51)
                .addComponent(jButton6)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        registryDIalogLayout.setVerticalGroup(
            registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(registryDIalogLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(registryDIalogLayout.createSequentialGroup()
                        .addGroup(registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addGap(18, 18, 18)
                        .addGroup(registryDIalogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, registryDIalogLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton6)))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        memoryDialog.setTitle("Memorija");
        memoryDialog.setAlwaysOnTop(true);
        memoryDialog.setMinimumSize(new java.awt.Dimension(530, 400));
        memoryDialog.setModal(true);
        memoryDialog.setName("memDialog"); // NOI18N
        memoryDialog.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                memoryDialogWindowClosed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Adresa", "0", "1", "2", "3", "4", "5", "6", "7"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Long.class, java.lang.Long.class, java.lang.Long.class, java.lang.Long.class, java.lang.Long.class, java.lang.Long.class, java.lang.Long.class, java.lang.Long.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true, true, true, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setColumnSelectionAllowed(true);
        jScrollPane1.setViewportView(jTable1);

        jTextField10.setText("0000");
        jTextField10.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                enterPressed(evt);
            }
        });

        jLabel9.setText("Pocetna adresa:");

        pomeriAdresu.setText("Učitaj početnu adresu");
        pomeriAdresu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pomeriAdresuActionPerformed(evt);
            }
        });

        jButton8.setText("Sačuvaj");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout memoryDialogLayout = new javax.swing.GroupLayout(memoryDialog.getContentPane());
        memoryDialog.getContentPane().setLayout(memoryDialogLayout);
        memoryDialogLayout.setHorizontalGroup(
            memoryDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(memoryDialogLayout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(memoryDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(memoryDialogLayout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(pomeriAdresu)))
                .addContainerGap(110, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, memoryDialogLayout.createSequentialGroup()
                .addContainerGap(480, Short.MAX_VALUE)
                .addComponent(jButton8)
                .addGap(42, 42, 42))
        );
        memoryDialogLayout.setVerticalGroup(
            memoryDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(memoryDialogLayout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(memoryDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pomeriAdresu))
                .addGap(34, 34, 34)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addComponent(jButton8)
                .addContainerGap(102, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("AOR2 Simulator");
        setAlwaysOnTop(true);
        setModal(true);
        setName("mainDialog"); // NOI18N

        buttonGroup1.add(radioProgram);
        radioProgram.setSelected(true);
        radioProgram.setLabel("Ucitavanje programa");
        radioProgram.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioProgramActionPerformed(evt);
            }
        });

        buttonGroup1.add(radioFajl);
        radioFajl.setLabel("Ucitavanje stanja sistema ");
        radioFajl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioFajlActionPerformed(evt);
            }
        });

        src.setEnabled(false);
        src.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                srcActionPerformed(evt);
            }
        });

        open.setText("Otvori");
        open.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openActionPerformed(evt);
            }
        });

        exit.setText("Izadji");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });

        simButton.setText("Simulacija");
        simButton.setToolTipText("");
        simButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simButtonActionPerformed(evt);
            }
        });

        initReg.setLabel("Inicijalizacija registara");
        initReg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                initRegActionPerformed(evt);
            }
        });

        initMem.setLabel("Inicijalizacija memorije");
        initMem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                initMemActionPerformed(evt);
            }
        });

        jButton1.setText("Snimi");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(initMem)
                    .addComponent(initReg)
                    .addComponent(radioFajl)
                    .addComponent(radioProgram)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(src, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(exit, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                                    .addComponent(open)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                                .addGap(49, 49, 49)
                                .addComponent(simButton, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                                .addGap(146, 146, 146)))
                        .addGap(63, 63, 63)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(radioProgram)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(radioFajl)
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(src, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(open))
                .addGap(44, 44, 44)
                .addComponent(initReg)
                .addGap(26, 26, 26)
                .addComponent(initMem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(simButton)
                    .addComponent(exit))
                .addGap(40, 40, 40))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void srcActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_srcActionPerformed
	// TODO add your handling code here:
	}// GEN-LAST:event_srcActionPerformed

	private void radioFajlActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_radioFajlActionPerformed
	// TODO add your handling code here:
		initReg.setEnabled(false);
		initMem.setEnabled(false);
	}// GEN-LAST:event_radioFajlActionPerformed

	private void openActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_openActionPerformed
		int returnVal = init_file.showOpenDialog(this);
		File file_asm_state;
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file_asm_state = init_file.getSelectedFile();
			src.setText(file_asm_state.getPath());
		}
	}// GEN-LAST:event_openActionPerformed

	private void radioProgramActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_radioProgramActionPerformed
		// TODO add your handling code here:
		initReg.setEnabled(true);
		initMem.setEnabled(true);

	}// GEN-LAST:event_radioProgramActionPerformed

	private void exitActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_exitActionPerformed
		// TODO add your handling code here:
		System.exit(0);
	}// GEN-LAST:event_exitActionPerformed

	private void simButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_simButtonActionPerformed
		// TODO add your handling code here:
		if (radioProgram.isSelected()) {
			String fname = src.getText();
			if (fname.equals(""))
				Assembler.load("src/aor2/assembly/amsTest1.txt",
						CpuFrame.getInstance().mem, 0);
			else
				Assembler.load(fname, CpuFrame.getInstance().mem, 0);// XXX da
																		// li
																		// krecemo
																		// od 0?
		} else {
			if (src.getText().equals("")) {
				JOptionPane.showMessageDialog(null, "Morate izabrati fajl!");
				return;
			}
			Scanner allStates = null;
			try {
				allStates = new Scanner(init_file.getSelectedFile());
				allStates.useDelimiter("<#delim#>");
				ClockCPU.getInstance().restore(allStates.next());
				InternBus.M1.restore(allStates.next());
				ConditionSignalSet.getInstance().restore(allStates.next());
				InterruptSignalSet.getInstance().restore(allStates.next());
				ControlSignalSet.getInstance().restore(allStates.next());
				ControlUnitHorizontal2Type.getInstance().restore(
						allStates.next());
				Arbitrator.getInstance().restore(allStates.next());
				BusOvertake.getInstance().restore(allStates.next());
				InterfaceBlock.getInstance().restore(allStates.next());
				SynchronizationBlock.getInstance().restore(allStates.next());
				AlushiftXYM1.getInstance().restore(allStates.next());
				Psw.getInstance().restore(allStates.next());
				GPRegisters.R0.restore(allStates.next());
				InstructionRegister.IR0.restore(allStates.next());
				InterruptBlock.getInstance().restore(allStates.next());
				TempRegisters.A.restore(allStates.next());
				ADbus.ABus.restore(allStates.next());
				SystemBus.getInstance().restore(allStates.next());
				CpuFrame.getInstance().mem.restore(allStates.next());
			} catch (Exception e) {
				e.printStackTrace();
			}

			finally {
				if (allStates != null)
					allStates.close();
			}
		}
		this.dispose();
		init_file.setVisible(false);
	}// GEN-LAST:event_simButtonActionPerformed

	private void initRegActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_initRegActionPerformed

		registryDIalog.setLocation(400, 150);
		registryDIalog.setSize(300, 500);
		jTextField2.setText(Long
				.toHexString(GPRegisters.R0.getState() & 0xFFFF).toUpperCase());
		jTextField3.setText(Long
				.toHexString(GPRegisters.R1.getState() & 0xFFFF).toUpperCase());
		jTextField4.setText(Long
				.toHexString(GPRegisters.R2.getState() & 0xFFFF).toUpperCase());
		jTextField5.setText(Long
				.toHexString(GPRegisters.R3.getState() & 0xFFFF).toUpperCase());
		jTextField6.setText(Long
				.toHexString(GPRegisters.R4.getState() & 0xFFFF).toUpperCase());
		jTextField7.setText(Long
				.toHexString(GPRegisters.R5.getState() & 0xFFFF).toUpperCase());
		jTextField8.setText(Long
				.toHexString(GPRegisters.R6.getState() & 0xFFFF).toUpperCase());
		jTextField9.setText(Long
				.toHexString(GPRegisters.R7.getState() & 0xFFFF).toUpperCase());
		jTextField11
				.setText(Long.toHexString(
						InterruptBlock.getInstance().getImr().getState())
						.toUpperCase());
		registryDIalog.setVisible(true);

	}// GEN-LAST:event_initRegActionPerformed

	private void initMemActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_initMemActionPerformed

		memoryDialog.setLocation(300, 150);

		jTextField10.setText("0000");
		for (int i = 0; i < 10; i++) {
			jTable1.setValueAt(Integer.toHexString(8 * i), i, 0);
		}

		for (int i = 0; i < 10; i++) {
			for (int j = 1; j < 9; j++)
				jTable1.setValueAt(
						CpuFrame.getInstance().mem.readWord(i * 8 + j - 1), i,
						j);
		}
		memoryDialog.setVisible(true);
	}// GEN-LAST:event_initMemActionPerformed

	private void pomeriAdresuActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton7ActionPerformed
		// FIXME memorija nam je dekadna!!!
		int address = Integer.parseInt(jTextField10.getText(), 16);
                if (address>=CpuFrame.getInstance().mem.capacity()) {
                    JOptionPane.showMessageDialog(memoryDialog,
					"ADRESA NE POSTOJI U MEMORIJI!!!");
                    return;
                }
		address -= (address % 8);
		for (int i = 0; i < 10; i++)
			for (int j = 1; j < 9; j++)
				CpuFrame.getInstance().mem.writeWord(
						Integer.parseInt((String) jTable1.getValueAt(i, 0), 16)
								+ j - 1, (Long) jTable1.getValueAt(i, j));

		for (int i = 0; i < 10; i++)
			jTable1.setValueAt(Integer.toHexString(address + (0x8) * i), i, 0);

		for (int i = 0; i < 10; i++)
			for (int j = 1; j < 9; j++)
				jTable1.setValueAt(
						CpuFrame.getInstance().mem.readWord((int) (address + i
								* 8 + j - 1)), i, j);

	}// GEN-LAST:event_jButton7ActionPerformed

	private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton6ActionPerformed
		// TODO add your handling code here:
		GPRegisters.R0.setState(Long.parseLong(jTextField2.getText(), 16));
		GPRegisters.R1.setState(Long.parseLong(jTextField3.getText(), 16));
		GPRegisters.R2.setState(Long.parseLong(jTextField4.getText(), 16));
		GPRegisters.R3.setState(Long.parseLong(jTextField5.getText(), 16));
		GPRegisters.R4.setState(Long.parseLong(jTextField6.getText(), 16));
		GPRegisters.R5.setState(Long.parseLong(jTextField7.getText(), 16));
		GPRegisters.R6.setState(Long.parseLong(jTextField8.getText(), 16));
		GPRegisters.R7.setState(Long.parseLong(jTextField9.getText(), 16));
		InterruptBlock.getInstance().getImr()
				.setState(Long.parseLong(jTextField11.getText(), 16));

		registryDIalog.dispose();
	}// GEN-LAST:event_jButton6ActionPerformed

	private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton8ActionPerformed
		for (int i = 0; i < 10; i++)
			for (int j = 1; j < 9; j++)
				CpuFrame.getInstance().mem.writeWord(
						Integer.parseInt((String) jTable1.getValueAt(i, 0), 16)
								+ j - 1, (Long) jTable1.getValueAt(i, j));
		memoryDialog.setVisible(false);
	}// GEN-LAST:event_jButton8ActionPerformed

	private void enterPressed(java.awt.event.KeyEvent evt) {// GEN-FIRST:event_enterPressed
		if (evt.getKeyCode() == KeyEvent.VK_ENTER)
			pomeriAdresu.doClick();
	}// GEN-LAST:event_enterPressed

	/*
	 * public JDialog registryDial() { return registryDIalog; }
	 * 
	 * public JDialog memoryDial() { return memoryDialog; }
	 */

	public void readOnlyReg() {
		registryDIalog.setLocation(300, 100);
		registryDIalog.setSize(300, 500);
		jTextField2.setText(Long
				.toHexString(GPRegisters.R0.getState() & 0xFFFF).toUpperCase());
		jTextField3.setText(Long
				.toHexString(GPRegisters.R1.getState() & 0xFFFF).toUpperCase());
		jTextField4.setText(Long
				.toHexString(GPRegisters.R2.getState() & 0xFFFF).toUpperCase());
		jTextField5.setText(Long
				.toHexString(GPRegisters.R3.getState() & 0xFFFF).toUpperCase());
		jTextField6.setText(Long
				.toHexString(GPRegisters.R4.getState() & 0xFFFF).toUpperCase());
		jTextField7.setText(Long
				.toHexString(GPRegisters.R5.getState() & 0xFFFF).toUpperCase());
		jTextField8.setText(Long
				.toHexString(GPRegisters.R6.getState() & 0xFFFF).toUpperCase());
		jTextField9.setText(Long
				.toHexString(GPRegisters.R7.getState() & 0xFFFF).toUpperCase());
		jTextField11
				.setText(Long.toHexString(
						InterruptBlock.getInstance().getImr().getState())
						.toUpperCase());
		jTextField2.setEditable(false);
		jTextField3.setEditable(false);
		jTextField4.setEditable(false);
		jTextField5.setEditable(false);
		jTextField6.setEditable(false);
		jTextField7.setEditable(false);
		jTextField8.setEditable(false);
		jTextField9.setEditable(false);
		jTextField11.setEditable(false);
		jButton6.setEnabled(false);
		registryDIalog.setVisible(true);
	}

	public void revertreadOnlyReg() {

		jTextField2.setEditable(true);
		jTextField3.setEditable(true);
		jTextField4.setEditable(true);
		jTextField5.setEditable(true);
		jTextField6.setEditable(true);
		jTextField7.setEditable(true);
		jTextField8.setEditable(true);
		jTextField9.setEditable(true);
		jTextField11.setEditable(true);
		jButton6.setEnabled(true);
		registryDIalog.setVisible(false);
	}

	public void readOnlyMem() {
		memoryDialog.setLocationRelativeTo(simButton);

		jTextField10.setText("0000");
		for (int i = 0; i < 10; i++) {
			jTable1.setValueAt(Integer.toHexString(8 * i), i, 0);
		}

		for (int i = 0; i < 10; i++) {
			for (int j = 1; j < 9; j++)
				jTable1.setValueAt(
						CpuFrame.getInstance().mem.readWord(i * 8 + j - 1), i,
						j);
		}
		jButton8.setEnabled(false);
		jTable1.setEnabled(false);
		memoryDialog.setVisible(true);
	}

	public void revertreadOnlyMem() {
		jButton8.setEnabled(true);
		jTable1.setEnabled(true);
		memoryDialog.setVisible(false);
	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
		// TODO add your handling code here:
                JFileChooser saveFile = CpuFrame.getInstance().getSaveDialog();
		int returnVal = saveFile.showSaveDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File toSave = saveFile.getSelectedFile();
			toSave.setWritable(true);
			FileWriter output = null;
			try {
				output = new FileWriter(toSave);
				output.append("<#delim#>\n"
						+ ClockCPU.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(InternBus.M1.createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(ConditionSignalSet.getInstance()
						.createRestorePoint() + "\n<#delim#>\n");
				output.append(InterruptSignalSet.getInstance()
						.createRestorePoint() + "\n<#delim#>\n");
				output.append(ControlSignalSet.getInstance()
						.createRestorePoint() + "\n<#delim#>\n");
				output.append(ControlUnitHorizontal2Type.getInstance()
						.createRestorePoint() + "\n<#delim#>\n"); // FIXME ovo
																	// samo kada
																	// je horiz.
																	// uprav.
																	// jed.
				output.append(Arbitrator.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(BusOvertake.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(InterfaceBlock.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(SynchronizationBlock.getInstance()
						.createRestorePoint() + "\n<#delim#>\n");
				output.append(AlushiftXYM1.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(Psw.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(GPRegisters.R0.createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(InstructionRegister.IR0.createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(InterruptBlock.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(TempRegisters.A.createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(ADbus.ABus.createRestorePoint() + "\n<#delim#>\n");
				output.append(SystemBus.getInstance().createRestorePoint()
						+ "\n<#delim#>\n");
				output.append(CpuFrame.getInstance().mem.createRestorePoint() + "\n");
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (output != null) {
						output.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
                
	}// GEN-LAST:event_jButton1ActionPerformed

	private void registryDIalogWindowClosed(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_registryDIalogWindowClosed
		// TODO add your handling code here:
		revertreadOnlyReg();
	}// GEN-LAST:event_registryDIalogWindowClosed

	private void memoryDialogWindowClosed(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_memoryDialogWindowClosed
		// TODO add your handling code here:
		revertreadOnlyMem();
	}// GEN-LAST:event_memoryDialogWindowClosed

	/**
	 * @param args
	 *            the command line arguments
	 */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton exit;
    private javax.swing.JButton initMem;
    private javax.swing.JButton initReg;
    private javax.swing.JFileChooser init_file;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField11;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    private javax.swing.JDialog memoryDialog;
    private javax.swing.JButton open;
    private javax.swing.JButton pomeriAdresu;
    private javax.swing.JRadioButton radioFajl;
    private javax.swing.JRadioButton radioProgram;
    private javax.swing.JDialog registryDIalog;
    private javax.swing.JButton simButton;
    private javax.swing.JTextField src;
    // End of variables declaration//GEN-END:variables
}
